import 'package:citiesocial/http/http.dart';
import 'package:citiesocial/_include.dart';
import 'package:citiesocial/beans/agreement_entity.dart';

class HttpManager {
  static const int default_size = 10;
  static final HttpManager _singleton = HttpManager._internal();
  HttpManager._internal();
  factory HttpManager() => _singleton;

  ///首页轮播图
  static Future<List<BannerEntity>> indexBanner({BuildContext context}) async {
    var _httpResult = await Http.get('index_banner', context: context);
    var result = List<BannerEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(BannerEntity.fromJson(item));
    }
    return result;
  }

  ///即时排行（top100）
  static Future<List<GoodsEntity>> getHotGoods(
      {BuildContext context, int page = 1, int pagesize = default_size}) async {
    var _httpResult = await Http.get(
        'get_hot_goods&page=$page&pagesize=$pagesize',
        context: context);
    var result = List<GoodsEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(GoodsEntity.fromJson(item));
    }
    return result;
  }

  ///促销活动列表，对应首页闪购（包括最新闪购，热卖闪购，即将结束）
  static Future<IndexEntity> shopActivity(String type, String equip,
      {BuildContext context, int page = 1, int pagesize = default_size}) async {
    var _httpResult = await Http.get(
        'shop_activity&type=$type&page=$page&pagesize=$pagesize&equip=$equip',
        context: context);
    IndexEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = IndexEntity.fromJson(_httpData);
    return result;
  }

  ///修改闪购到时提醒
  ///state 提醒状态（1-打开通知 0-关闭通知）
  static Future<bool> remind(String state, String equip, String activity_id,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'remind&state=$state&equip=$equip&activity_id=$activity_id',
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///通过闪购活动id，获取闪购活动详情
  static Future<ActicityDetailEntity> activityDetail(
      String activity_id, String equip,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'activity_detail&activity_id=$activity_id&equip=$equip',
        context: context);
    ActicityDetailEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = ActicityDetailEntity.fromJson(_httpData);
    return result;
  }

  ///主题活动列表
  ///对应首页逛逛主题和分类页顶部
  static Future<List<ThemeEntity>> themes(
      {BuildContext context, int page = 1, int pagesize = default_size}) async {
    var _httpResult = await Http.get('themes&page=$page&pagesize=$pagesize',
        context: context);
    var result = List<ThemeEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(ThemeEntity.fromJson(item));
    }
    return result;
  }

  ///通过主题id获取商品列表
  static Future<ThemeEntity> goodsListFromTheme(String theme_id, String equip,
      {BuildContext context, int page = 1, int pagesize = default_size}) async {
    var _httpResult = await Http.get(
        'goods_list_from_theme&page=$page&pagesize=$pagesize&theme_id=$theme_id&equip=$equip',
        context: context);
    ThemeEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = ThemeEntity.fromJson(_httpData);
    return result;
  }

  ///最近浏览历史商品
  static Future<List<GoodsEntity>> historyGoods(String equip,
      {BuildContext context, int page = 1, int pagesize = default_size}) async {
    var _httpResult = await Http.get(
        'history_goods&page=$page&pagesize=$pagesize&equip=$equip',
        context: context);
    var result = List<GoodsEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(GoodsEntity.fromJson(item));
    }
    return result;
  }

  ///添加浏览商品记录
  static Future<bool> addHistoryGoods(String equip, String goods_id,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'add_history_goods&equip=$equip&goods_id=$goods_id',
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///首页热门分类列表
  static Future<List<CategoryEntity>> hotCategory(
      {BuildContext context}) async {
    var _httpResult = await Http.get('hot_category', context: context);
    var result = List<CategoryEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(CategoryEntity.fromJson(item));
    }
    return result;
  }

  ///通过类别id，获取商品列表
  static Future<List<GoodsEntity>> goodsListFromCategory(
      String cat_id, String level, String equip,
      {BuildContext context, int page = 1, int pagesize = default_size}) async {
    var _httpResult = await Http.get(
        'goods_list_from_category&page=$page&pagesize=$pagesize&cat_id=$cat_id&level=$level&equip=$equip',
        context: context);
    var result = List<GoodsEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(GoodsEntity.fromJson(item));
    }
    return result;
  }

  ///商品详情
  static Future<GoodsDetailEntity> goodsDetail(String goods_id, String equip,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'goods_detail&goods_id=$goods_id&equip=$equip',
        context: context);
    GoodsDetailEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = GoodsDetailEntity.fromJson(_httpData);
    return result;
  }

  ///今日热卖，热卖商品
  static Future<List<GoodsEntity>> hotGoods(String equip,
      {BuildContext context, int page = 1, int pagesize = default_size}) async {
    var _httpResult = await Http.get('hot_goods&page=$page&pagesize=$pagesize',
        context: context);
    var result = List<GoodsEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(GoodsEntity.fromJson(item));
    }
    return result;
  }

  ///获取促销活动类型
  static Future<List<ActivityTypeEntity>> shopActivityType(
      {BuildContext context}) async {
    var _httpResult = await Http.get('shop_activity_type', context: context);
    var result = List<ActivityTypeEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(ActivityTypeEntity.fromJson(item));
    }
    return result;
  }

  ///搜索商品
  static Future<List<GoodsEntity>> searchGoods(String keyword,
      {BuildContext context, int page = 1, int pagesize = default_size}) async {
    var _httpResult = await Http.get(
        'search_goods&page=$page&pagesize=$pagesize&keyword=$keyword',
        context: context);
    var result = List<GoodsEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(GoodsEntity.fromJson(item));
    }
    return result;
  }

  ///获取闪购中类别
  static Future<List<CategoryEntity>> category({BuildContext context}) async {
    var _httpResult = await Http.get('category', context: context);
    var result = List<CategoryEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(CategoryEntity.fromJson(item));
    }
    return result;
  }

  ///获取某分类对应的闪购活动列表
  static Future<List<ActicityEntity>> flashSalesList(
      String cat_id, String equip,
      {BuildContext context, int page = 1, int pagesize = default_size}) async {
    var _httpResult = await Http.get(
        'flash_sales_list&page=$page&pagesize=$pagesize&cat_id=$cat_id&equip=$equip',
        context: context);
    var result = List<ActicityEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(ActicityEntity.fromJson(item));
    }
    return result;
  }

  ///获取目录中一级类别
  static Future<List<CategoryEntity>> directoryCategory(
      {BuildContext context}) async {
    var _httpResult = await Http.get('directory_category', context: context);
    var result = List<CategoryEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(CategoryEntity.fromJson(item));
    }
    return result;
  }

  ///获取目录中二三级分类
  static Future<List<CategoryEntity2>> directoryTwoCategory(String cat_id,
      {BuildContext context}) async {
    var _httpResult = await Http.get('directory_two_category&cat_id=$cat_id',
        context: context);
    var result = List<CategoryEntity2>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(CategoryEntity2.fromJson(item));
    }
    return result;
  }

  ///加入收藏
  static Future<bool> addFocus(
      String goods_id, String user_id, String user_token,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'add_focus&goods_id=$goods_id&user_id=$user_id&user_token=$user_token',
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///收藏的商品列表
  static Future<List<GoodsEntity>> focusList(String user_id, String user_token,
      {BuildContext context, int page = 1, int pagesize = default_size}) async {
    var _httpResult = await Http.get(
        'focus_list&page=$page&pagesize=$pagesize&user_id=$user_id&user_token=$user_token',
        context: context);
    var result = List<GoodsEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(GoodsEntity.fromJson(item));
    }
    return result;
  }

  ///取消收藏
  static Future<bool> deleteFocus(
      String goods_id, String user_id, String user_token,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'delete_focus&goods_id=$goods_id&user_id=$user_id&user_token=$user_token',
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///取消收藏
  ///goods_id 商品id,多个商品用逗号分隔
  static Future<bool> deleteMoreFocus(
      String goods_id, String user_id, String user_token,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'delete_more_focus&goods_id=$goods_id&user_id=$user_id&user_token=$user_token',
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///获取邮件验证码
  static Future<bool> getVerify(String email, {BuildContext context}) async {
    var _httpResult =
        await Http.get('get_verify&email=$email', context: context);
    // return _httpResult?.data ?? '';
    return _httpResult?.error_code == '0';
  }

  ///用户注册接口
  static Future<UserEntity> register(String email, String password,
      String surname, String name, String code, String equip,
      {BuildContext context}) async {
    var _httpResult = await Http.post('register',
        body: {
          'email': '$email',
          'password': '$password',
          'surname': '$surname',
          'name': '$name',
          'code': '$code',
          'equip': '$equip'
        },
        context: context);
    UserEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = UserEntity.fromJson(_httpData);
    return result;
  }

  ///用户登录接口
  static Future<UserEntity> login(String email, String password, String equip,
      {BuildContext context}) async {
    var _httpResult = await Http.post('login',
        body: {'email': '$email', 'password': '$password', 'equip': '$equip'},
        context: context);
    UserEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = UserEntity.fromJson(_httpData);
    return result;
  }

  ///忘记密码
  static Future<UserEntity> forgetPassword(
      String email, String password, String code, String equip,
      {BuildContext context}) async {
    var _httpResult = await Http.post('forget_password',
        body: {
          'email': '$email',
          'password': '$password',
          'code': '$code',
          'equip': '$equip'
        },
        context: context);
    UserEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = UserEntity.fromJson(_httpData);
    return result;
  }

  ///获取所有国家
  static Future<List<CountryEntity>> country({BuildContext context}) async {
    var _httpResult = await Http.get('country', context: context);
    var result = List<CountryEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(CountryEntity.fromJson(item));
    }
    return result;
  }

  ///通过国家获取所有区域
  static Future<List<AreaEntity>> area(String id,
      {BuildContext context}) async {
    var _httpResult = await Http.get('area&id=$id', context: context);
    var result = List<AreaEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(AreaEntity.fromJson(item));
    }
    return result;
  }

  ///通过国家获取所有区域
  static Future<List<CityEntity>> city(String id,
      {BuildContext context}) async {
    var _httpResult = await Http.get('city&id=$id', context: context);
    var result = List<CityEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(CityEntity.fromJson(item));
    }
    return result;
  }

  ///新增收货地址接口
  static Future<bool> addAddress(
      String user_id,
      String user_token,
      String surname,
      String name,
      String country,
      String province,
      String city,
      String address,
      String mobile,
      {BuildContext context}) async {
    var _httpResult = await Http.post('add_address',
        body: {
          'user_id': '$user_id',
          'user_token	': '$user_token	',
          'surname': '$surname',
          'name': '$name',
          'country': '$country',
          'province': '$province',
          'city': '$city',
          'address': '$address',
          'mobile': '$mobile',
        },
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///收货地址列表
  static Future<List<AddressEntity>> addressList(
      String user_id, String user_token,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'address_list&user_id=$user_id&user_token=$user_token',
        context: context);
    var result = List<AddressEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(AddressEntity.fromJson(item));
    }
    return result;
  }

  ///收货地址详情
  static Future<AddressEntity> addressDetail(
      String user_id, String user_token, String address_id,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'address_detail&user_id=$user_id&user_token=$user_token&address_id=$address_id',
        context: context);
    AddressEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = AddressEntity.fromJson(_httpData);
    return result;
  }

  ///修改收货地址接口
  static Future<bool> updateAddress(
      String user_id, String user_token, String address_id,
      {String surname,
      String name,
      String country,
      String province,
      String city,
      String street,
      String address,
      String mobile,
      BuildContext context}) async {
    var _httpResult = await Http.post('update_address',
        body: {
          'user_id': '$user_id',
          'user_token': '$user_token',
          'address_id': '$address_id',
          'surname': '$surname',
          'name': '$name',
          'country': '$country',
          'province': '$province',
          'city': '$city',
          'street': '$street',
          'address': '$address',
          'mobile': '$mobile',
        },
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///删除收货地址
  static Future<bool> deleteAddress(
      String user_id, String user_token, String address_id,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'delete_address&user_id=$user_id&user_token=$user_token&address_id=$address_id',
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///设置主要地址
  static Future<bool> updateMainAddress(
      String user_id, String user_token, String address_id,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'update_main_address&user_id=$user_id&user_token=$user_token&address_id=$address_id',
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///最近推播通知
  static Future<List<SystemMessageEntity>> systemMessage(
      String user_id, String user_token,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'system_message&user_id=$user_id&user_token=$user_token',
        context: context);
    var result = List<SystemMessageEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(SystemMessageEntity.fromJson(item));
    }
    return result;
  }

  ///推播设定接口
  ///  return is_push 是否接受通知(1-接受 0-否)
  static Future<SystemMessageSetEntity> systemMessageSet(
      String user_id, String user_token,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'system_message_set&user_id=$user_id&user_token=$user_token',
        context: context);
    SystemMessageSetEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = SystemMessageSetEntity.fromJson(_httpData);
    return result;
  }

  ///修改推播设定
  ///  return is_push 是否接受通知(1-接受 0-否)
  static Future<SystemMessageSetEntity> updateSystemMessageSet(
      String user_id, String user_token,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'update_system_message_set&user_id=$user_id&user_token=$user_token',
        context: context);
    SystemMessageSetEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = SystemMessageSetEntity.fromJson(_httpData);
    return result;
  }

  ///客服中心
  static Future<CustomerServiceEntity> customerService(
      {BuildContext context}) async {
    var _httpResult = await Http.get('customer_service', context: context);
    CustomerServiceEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = CustomerServiceEntity.fromJson(_httpData);
    return result;
  }

  ///添加到购物车接口
  static Future<bool> addCart(
      String user_id, String user_token, String goods_id,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'add_cart&user_id=$user_id&user_token=$user_token&goods_id=$goods_id',
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///减少购物车数量
  static Future<bool> reduceCart(
      String user_id, String user_token, String rec_id,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'reduce_cart&user_id=$user_id&user_token=$user_token&rec_id=$rec_id',
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///购物车列表
  static Future<CartEntity> cartList(String user_id, String user_token,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'cart_list&user_id=$user_id&user_token=$user_token',
        context: context);
    CartEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = CartEntity.fromJson(_httpData);
    return result;
  }

  ///删除购物车中的某个商品
  static Future<bool> deleteCart(
      String user_id, String user_token, String rec_id,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'delete_cart&user_id=$user_id&user_token=$user_token&rec_id=$rec_id',
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///批量删除购物车中的商品
  static Future<bool> deleteMoreCart(
      String user_id, String user_token, String rec_id,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'delete_more_cart&user_id=$user_id&user_token=$user_token&rec_id=$rec_id',
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///获取默认收货地址
  static Future<AddressEntity> defaultAddress(String user_id, String user_token,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'default_address&user_id=$user_id&user_token=$user_token',
        context: context);
    AddressEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = AddressEntity.fromJson(_httpData);
    return result;
  }

  ///确认下单接口, 此处的OrderEntity只返回order_id和order_sn
  static Future<OrderEntity> sureOrder(String user_id, String user_token,
      String email, String total, String pay_status, String address_id,
      {BuildContext context}) async {
    var _httpResult = await Http.post('sureOrder',
        body: {
          'user_id': '$user_id',
          'user_token': '$user_token',
          'email': '$email',
          'total': '$total',
          'pay_status': '$pay_status', //支付方式（1-微信 2-支付宝）
          'address_id': '$address_id',
        },
        context: context);
    OrderEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = OrderEntity.fromJson(_httpData);
    return result;
  }

  ///我的订单
  static Future<List<OrderEntity>> orderAll(
      String user_id, String user_token, String state,
      {int page = 1, int pagesize = default_size, BuildContext context}) async {
    var _httpResult = await Http.get(
        'order_all&user_id=$user_id&user_token=$user_token&state=$state&page=$page&pagesize=$pagesize',
        context: context);
    var result = List<OrderEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(OrderEntity.fromJson(item));
    }
    return result;
  }

  ///订单详情接口
  static Future<OrderDetailEntity> orderDetail(
      String user_id, String user_token, String order_id,
      {BuildContext context}) async {
    var _httpResult = await Http.get(
        'order_detail&user_id=$user_id&user_token=$user_token&order_id=$order_id',
        context: context);
    OrderDetailEntity result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = OrderDetailEntity.fromJson(_httpData);
    return result;
  }

  ///订单详情（为你推荐）
  static Future<List<GoodsEntity>> recommend(
      {BuildContext context,
      String user_id,
      String user_token,
      String order_id}) async {
    var _httpResult = await Http.get(
        'recommend&user_id=$user_id&user_token=$user_token&order_id=$order_id',
        context: context);
    var result = List<GoodsEntity>();
    List<dynamic> _httpData = _httpResult?.data ?? [];
    for (dynamic item in _httpData) {
      result.add(GoodsEntity.fromJson(item));
    }
    return result;
  }

  ///点击付款
  static Future<bool> pay(
      String user_id, String user_token, String order_id, String total,
      {BuildContext context}) async {
    var _httpResult = await Http.post('pay',
        body: {
          'user_id': '$user_id',
          'user_token': '$user_token',
          'order_id': '$order_id',
          'total': '$total', //订单总金额
        },
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///点击取消
  static Future<bool> cancel(String user_id, String user_token, String order_id,
      {BuildContext context}) async {
    var _httpResult = await Http.post('cancel',
        body: {
          'user_id': '$user_id',
          'user_token': '$user_token',
          'order_id': '$order_id',
        },
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///点击取消
  static Future<bool> sureGoods(
      String user_id, String user_token, String order_id,
      {BuildContext context}) async {
    var _httpResult = await Http.post('sure_goods',
        body: {
          'user_id': '$user_id',
          'user_token': '$user_token',
          'order_id': '$order_id',
        },
        context: context);
    return _httpResult?.error_code == '0';
  }

  ///用户协议
  static Future<Agreement> userAgreement([BuildContext context]) async {
    var _httpResult = await Http.get('user_agreement', context: context);
    Agreement result;
    dynamic _httpData = _httpResult?.data ?? {};
    result = Agreement.fromJson(_httpData);
    return result;
  }

  // ///xxxxxx
  // static Future<List<XXX>> xxx(String id, {BuildContext context}) async {
  //   var _httpResult = await Http.get('xxx&id=$id', context: context);
  //   var result = List<XXX>();
  //   List<dynamic> _httpData = _httpResult?.data ?? [];
  //   for (dynamic item in _httpData) {
  //     result.add(XXX.fromJson(item));
  //   }
  //   return result;
  // }

}
